package com.nervenets.template.controller.common;

import com.nervenets.general.aspect.SysLog;
import com.nervenets.general.entity.ResponsePageResult;
import com.nervenets.general.entity.ResponseResult;
import com.nervenets.general.jwt.util.JwtUtils;
import com.nervenets.general.model.SecurityUser;
import com.nervenets.general.web.BaseController;
import com.nervenets.template.controller.common.params.FavPagingParams;
import com.nervenets.template.controller.common.params.FavParams;
import com.nervenets.template.hibernate.domain.common.Fav;
import com.nervenets.template.service.FavService;
import com.nervenets.template.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@Api(value = "收藏接口v1", tags = "收藏相关接口")
@RestController
@RequestMapping("/api/fav")
public class FavController extends BaseController {
    @Resource
    private FavService favService;
    @Resource
    private UserService userService;

    @SysLog
    @ApiOperation("收藏")
    @PostMapping
    public ResponseEntity<ResponseResult<List<Long>>> fav(@Valid @RequestBody FavParams params) throws Exception {
        switch (params.getUnionType()) {
            case normal:
                break;
            case manager:
                break;
            case user:
                userService.findOne(params.getTargetId(), "不存在的用户");
                break;
            case rechargeOrder:
                break;
            case flows:
                break;
            case order:
                break;
            case product:
                break;
            case coupon:
                break;
        }
        return successMessage(favService.fav(JwtUtils.getUser().getId(), params.getUnionType(), params.getTargetId()));
    }

    @ApiOperation("收藏列表")
    @PostMapping("/list")
    public ResponseEntity<ResponsePageResult<Fav>> list(@Valid @RequestBody FavPagingParams params) {
        SecurityUser securityUser = JwtUtils.getUser();
        Page<Fav> page = favService.findAll(params, PageRequest.of(params.getPage(), params.getLimit(), Sort.by(Sort.Direction.DESC, "id")));
        List<Fav> favs = page.getContent();
        favs.forEach(fav -> {
            Object union = null;
            switch (fav.getType()) {
                case normal:
                    break;
                case manager:
                    break;
                case user:
                    break;
                case rechargeOrder:
                    break;
                case flows:
                    break;
                case order:
                    break;
                case product:
                    break;
                case coupon:
                    break;
            }
            fav.setUnion(union);
        });
        return successMessage(favs, params.getPage(), page.getTotalElements());
    }
}
